package com.boilerCloud.controller.sys;

import java.net.URLEncoder;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.HtmlUtils;

import com.boilerCloud.dataSource.LogAnnotation;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.dto.sys.User;
import com.boilerCloud.service.sys.IHzlqswRoleService;
import com.boilerCloud.service.sys.IHzlqswUserService;


/**
 * @类功能说明：用户登录
 * @类修改者：
 * @修改日期：
 * @修改说明：
 * @公司名称：杭州龙骞科技有限公司
 * @作者：qianfuqiang
 * @创建时间：2013-6-25 下午10:51:34
 * @版本：V1.0
 */
@RestController
public class HzlqswLoginControler {
	private static final Logger log = LoggerFactory.getLogger(HzlqswLoginControler.class);
	@Resource
	private IHzlqswRoleService menuService;
	@Resource
	private IHzlqswUserService userService;


	// 用户登录验证
	@LogAnnotation("登录系统")
	@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public  ReqResult doLogin(@RequestParam(required = true) String account,
			@RequestParam(required = true) String pwd,
			@RequestParam(required = true) String verifyCode,
			HttpServletRequest request,
			HttpServletResponse response) {
		ReqResult rst = new ReqResult();
		try {
			log.info("用户登录，帐号={}，密码={}", account, pwd);
			// 去除特殊字符
			account = HtmlUtils.htmlEscape(account);
			pwd = HtmlUtils.htmlEscape(pwd);
			verifyCode = HtmlUtils.htmlEscape(verifyCode);
			String checkverifyCode = request.getSession().getAttribute("randcode").toString();
			// 校对验证码
			if (verifyCode.isEmpty() || !verifyCode.equalsIgnoreCase(checkverifyCode)) {
				log.info("用户登录，帐号={}，验证码不通过", account);
				rst.setResultCode(ReqResult.resultCode_rand_error);
				rst.setReturnObject("验证码验证未通过");
				return rst;
			}
			// 校验帐号密码
			//if (true) {
				if (userService.checkUser(account, pwd.toUpperCase())) {
						// 获取用户数据
					User user = userService.getUserInfoByAccount(account);
					request.getSession().setAttribute("usersession", user);
					rst.setResultCode(ReqResult.resultCode_Success);
					rst.setReturnObject("用户验证通过");
					log.info("用户登录，帐号={}，认证通过", account);	
					Cookie cookie = new Cookie("username", URLEncoder.encode(user.getName(),"utf-8"));  
			        cookie.setMaxAge(60* 60*24*7);// 设置为7天
			        cookie.setPath("/");  
			        System.out.println("已添加===============");  
			        response.addCookie(cookie);
					return rst;
				}else {
					// 用户校验不通过
					log.info("用户登录，帐号={}，认证不通过", account);
					rst.setResultCode(ReqResult.resultCode_login_error);
					rst.setReturnObject("帐号校验未通过");
					return rst;
				}
			
		} catch (Exception e) {
			e.printStackTrace();
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("登录异常");
			return rst;
		}
	}

	

	
	// 登录后进去首页
	@RequestMapping(value = "/goIndex.do")
	public ModelAndView  goIndex(HttpServletRequest request) {
		
		return new ModelAndView("/views/index.html");
	}
	
	
	

	// 登录后进去left页面
	@RequestMapping(value = "/goLeft.do")
	public ModelAndView goLeft(HttpServletRequest request) {
		return new ModelAndView("/views/left.html");
	}
	
	@RequestMapping(value = "/goRight.do")
	public ModelAndView goRight(HttpServletRequest request) {
		return new ModelAndView("/views/right.html");
	}

	// 登录后进去top页面
	@RequestMapping(value = "/goTop.do")
	public ModelAndView goTop() {
		return new ModelAndView("/views/top.html");
	}

	

	// 无操作权限返回
	@RequestMapping(value = "/noLogin.do", produces = "text/plain;charset=UTF-8")
	public ReqResult noLogin() {
		ReqResult rst = new ReqResult();
		rst.setResultCode(ReqResult.resultCode_not_login);
		rst.setReturnObject("登录失效,请重新登录");
		return rst;
	}
	
	
	@LogAnnotation("退出系统")
	@RequestMapping(value = "/logout.do", produces = "text/plain;charset=UTF-8")
	public ReqResult logout(HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		request.getSession().invalidate();
		rst.setResultCode(ReqResult.resultCode_not_login);
		rst.setReturnObject("登录失效,请重新登录");
		return rst;
	}
	
	

	

}
